package com.example.zhengchaojun.arcgis.draw;

import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;

import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PolylineBuilder;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;
import com.example.zhengchaojun.arcgis.map.MapViewDelegate;

/**
 * 类描述：画点线面相关
 * 作者：郑朝军 on 2018/5/12
 * 邮箱：1250393285@qq.com
 * 公司：武汉智博创享科技有限公司
 * 修改人：郑朝军 on 2018/5/12
 * 修改备注：
 */
public class DrawTool
{
    protected MapView m_mapView;
    private GraphicsOverlay m_graphicsOverlay;


    private PolylineBuilder lineGeometry;

    public DrawTool(MapView mapView, MapViewDelegate delegate)
    {
        this.m_mapView = mapView;
        m_mapView.setOnTouchListener(new DefaultMapViewOnTouchListener(mapView.getContext(), mapView)
        {
            @Override
            public boolean onTouch(View view, MotionEvent event)
            {
                if (lineGeometry == null)
                {
                    addBoatTrip(event);
                }
                Point point = screenToLocation(event);
                lineGeometry.addPoint(point);
                return false;
            }
        });
        m_graphicsOverlay = new GraphicsOverlay();
        delegate.createGraphicsOverlay(m_graphicsOverlay);
    }

    private void addBuoyPoints(MotionEvent event)
    {
        Point point = screenToLocation(event);
        SimpleMarkerSymbol buoyMarker = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 10);
        Graphic buoyGraphic1 = new Graphic(point, buoyMarker);
        m_graphicsOverlay.getGraphics().add(buoyGraphic1);
    }

    private void addBoatTrip(MotionEvent event)
    {
        //define a polyline for the boat trip
        // line graphic
        lineGeometry = new PolylineBuilder(SpatialReferences.getWebMercator());
        // solid blue line symbol
        SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 5);
        // create graphic for polyline
        Graphic lineGraphic = new Graphic(lineGeometry.toGeometry());
        // create graphic overlay for polyline
        // create simple renderer
        SimpleRenderer lineRenderer = new SimpleRenderer(lineSymbol);
        // add graphic to overlay
        m_graphicsOverlay.setRenderer(lineRenderer);
        // add graphic to overlay
        m_graphicsOverlay.getGraphics().add(lineGraphic);
        // add graphics overlay to the MapView
    }

    private Point screenToLocation(MotionEvent event)
    {
        int x = (int) event.getX();
        int y = (int) event.getY();
        android.graphics.Point screen = new android.graphics.Point(x, y);
        return m_mapView.screenToLocation(screen);
    }
}
